package com.kotlin.network.retrofit.progress;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import com.kotlin.network.view.CustomProgressDialog;
import com.kotlin.network.view.LoadingDialog;

public class ProgressDialogHandler extends Handler {

    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;



    private Context context;
    private boolean cancelable;
    private ProgressCancelListener mProgressCancelListener;

    private ProgressDialog pd;
    private LoadingDialog mLoadingDialog;
    private CustomProgressDialog progressDialog = null;

    private int chooseDialog = 2;//Dialog选择

    public ProgressDialogHandler(Context context, ProgressCancelListener mProgressCancelListener,
                                 boolean cancelable) {
        super(Looper.getMainLooper());//必须执行在主线程
        this.context = context;
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
    }

    private void initProgressDialog(){
        if (pd == null && chooseDialog==0) {
            pd = new ProgressDialog(context);

            pd.setMessage("加载中...");
            pd.setCancelable(cancelable);

            if (cancelable) {
                pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        mProgressCancelListener.onCancelProgress();
                    }
                });
            }

            if (!pd.isShowing() && pd!=null) {
                try {
                    pd.show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        //第二种Dialog
        else if(mLoadingDialog == null && chooseDialog==1)
        {

            mLoadingDialog = new LoadingDialog();

            if (mLoadingDialog != null) {
                Activity activity = (Activity)context;
                try {
                    mLoadingDialog.show(activity.getFragmentManager(), "http");
                    //mLoadingDialog.show(GlobalApplication.gainContext(), "http");
                } catch (Exception e) {

                    Log.e("mLoadingDialog",e.toString());
                    e.printStackTrace();
                }
            }
        }

        //第三种Dialog 自定义 动画
        else if(progressDialog == null && chooseDialog==2){
            progressDialog = CustomProgressDialog.createDialog(context);
            progressDialog.setMessage("正在加载中...");
            try {
                progressDialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void dismissProgressDialog(){
        //第一种Dialog
        if (pd != null) {
            pd.dismiss();
            pd = null;
        }
        //第二种Dialog
        if (mLoadingDialog != null && mLoadingDialog.getShowsDialog() && mLoadingDialog.isAdded()) {
            try {
                mLoadingDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //第三种Dialog
        if (progressDialog != null){
            try {
                progressDialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
            progressDialog = null;
        }

    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }

}
